<?php
/**
 * 站点管理控制器
 * User: Lin Hui
 * Date: 2017/5/16
 * Time: 16:59
 */

namespace Admin\Controller;


use Common\Model\SettingModel;

class WebsiteController extends BaseController
{
    /**
     * 编辑页面添加信息
     */
    public function beforeEdit()
    {
        $this->assign('qiniu', getQiniuInfo());
    }

    /**
     * 编辑站点信息
     */
    public function index()
    {
        $model = new SettingModel();

        if(IS_POST) {
            $data = I('post.');
            if($data['weibo_url']) {
                $data['weibo_url'] = prefixUrl($data['weibo_url']);
            }

            if($data && $model->setSettingByPrefix('website_info', $data)) {
                $this->responseAjax('保存成功', true, array(), U(''));
            } else {
                $this->responseAjax('保存失败');
            }
        }

        $this->assign('page', C('WEBSITE.page'));
        $this->assign('info', $model->getWebsiteInfo());
        $this->assign('qiniu', getQiniuInfo());

        $this->display();
    }

    /**
     *页面列表
     */
    public function pages()
    {
        $pages = C('WEBSITE.page');
        $model = new SettingModel();
        foreach($pages as $key => $item) {
            $pages[$key] = array_merge($model->getPageInfo($key), array('page'=>$item));
        }
        $this->assign('pages', $pages);
        $this->display();
    }

    /**
     *设置页面信息
     */
    public function setPage()
    {
        $model = new SettingModel();
        $page = I('page');
        if(!$page) {
            $this->error('参数错误');
        }

        if(IS_POST) {
            if(
                $model->setSetting("website_info.page.{$page}.content", I('content')) &&
                $model->setSetting("website_info.page.{$page}.thumb", I('thumb')) &&
                $model->setSetting("website_info.page.{$page}.banner", I('banner'))
            ) {
                $this->responseAjax('保存成功', true, array(), U('pages'));
            } else {
                $this->responseAjax('保存失败');
            }
        }
        $pages = C('WEBSITE.page');
        $this->assign('pages', $pages);
        $this->assign('page', $page);
        $this->assign('info', $model->getPageInfo($page));
        $this->assign('qiniu', getQiniuInfo());
        $this->display();
    }

    /**
     * 设置首页视频
     */
    public function setVideo()
    {
        $set_model = new \Common\Model\SettingModel();
        $video = $set_model->getSetting('home_video');
        $video_thumb = $set_model->getSetting('home_video_thumb');

        if(IS_POST) {
            $video = I('video');
            $video_thumb = I('video_thumb');
            if($set_model->setSetting('home_video', $video)!== false && $set_model->setSetting('home_video_thumb', $video_thumb)!== false) {
                $this->responseAjax('保存成功', true);
            } else {
                $this->responseAjax('保存失败');
            }
        }

        $this->assign('qiniu', getQiniuInfo());
        $this->assign('video', $video);
        $this->assign('video_thumb', $video_thumb);
        $this->display();
    }
}